#include "Client.h"
#include "log.h"

bool Client::GetRegistrationData( PServerCommand pData, int nSize )
{
    if (pData == NULL || nSize < DATALEN)
    {
        return false;
    }

    PServerCommand pSC = pData;
    PGeneralRequest pCmd = (PGeneralRequest)pSC->data;
    pSC->cmd_type = Cmd_Register;
    pSC->cmd_size = sizeof(pCmd);

    if (!MakeGeneralData(pCmd))
    {
        FLogger.information("error occurred in GetRegistrationData");
        return false;
    }

    return true;
}

bool Client::Initialize(PBasicInformation pInfo)
{
    if (pInfo->user.length() > USR_LEN ||
        pInfo->pass.length() > PWD_LEN ||
        pInfo->hwid.length() != KEY_LEN)
    {
        cout << "wrong register data" << pInfo->user<<"|"<<pInfo->pkey<<"|" <<pInfo->pass<<"|" << pInfo->hwid<<"|" <<pInfo->email<<endl;
        return false;
    }

    SetUsername(pInfo->user);
    SetPassword(pInfo->pass);
    SetHardwareID(pInfo->hwid);
    SetProductKey(pInfo->pkey);
    SetEmailAddress(pInfo->email);

    return true;
}

void Client::UpdateRegistrationInfo( const PServerResponse pRes )
{
    PProductKeyInfo pInfo = (PProductKeyInfo)pRes->data;
    memcpy_s(&m_keyinfo, sizeof(ProductKeyInfo), pInfo, sizeof(ProductKeyInfo));
}

bool Client::StringSecurityCheck( const string s )
{
    return true;
}

bool Client::GetHeartBeatData( PServerCommand pData, int nSize )
{
    if (pData == NULL || nSize < DATALEN)
    {
        FLogger.information("invalid input data for heartbeat data");
        return false;
    }

    PServerCommand pSC = pData;
    PGeneralRequest pCmd = (PGeneralRequest)pSC->data;
    pSC->cmd_type = Cmd_Heartbeat;
    pSC->cmd_size = sizeof(GeneralRequest);

    if (!MakeGeneralData(pCmd))
    {
        FLogger.information("MakeGeneralData failed in GetHeartBeatData");
        return false;
    }

    return true;
}

bool Client::GetLoginData( PServerCommand pData, int nSize )
{
    if (pData == NULL || nSize < DATALEN)
    {
        FLogger.information("invalid input data for heartbeat data");
        return false;
    }

    PServerCommand pSC = pData;
    PGeneralRequest pCmd = (PGeneralRequest)pSC->data;
    pSC->cmd_type = Cmd_Login;
    pSC->cmd_size = sizeof(GeneralRequest);

    if (!MakeGeneralData(pCmd))
    {
        FLogger.information("MakeGeneralData failed in GetHeartBeatData");
        return false;
    }

    return true;
}

int Client::GetChangePasswordData(PServerCommand pSC,const string new_password)
{
    if (pSC == NULL || new_password == "" || new_password.length() < MIN_PWD_LEN || new_password.length() > PWD_LEN)
    {
        FLogger.information("invalid password format");
        return InvalidDataFormat;
    }

    pSC->cmd_type = Cmd_ChangePassword;
    pSC->cmd_size = sizeof(ChangePasswordRequest);
    PChangePasswordRequest pCPR = (PChangePasswordRequest)pSC->data;
    PGeneralRequest pGR = (PGeneralRequest)&pCPR->gr;

    if (!MakeGeneralData(pGR))
    {
        FLogger.information("MakeGeneralData failed in GetChangePasswordData");
        return Fail;
    }
    strcpy_s(pCPR->new_pass,   PWD_LEN+1, new_password.c_str());

    return Success;

}

int Client::GetUnBindData( PServerCommand pData )
{
    if (pData == NULL)
    {
        FLogger.information("wrong input data for Unbinding");
        return InvalidDataFormat;
    }

    pData->cmd_type = Cmd_UnBind;
    pData->cmd_size = sizeof(GeneralRequest);
    PGeneralRequest pGR = (PGeneralRequest)pData->data;

    if (!MakeGeneralData(pGR))
    {
        FLogger.information("MakeGeneralData failed in GetUnbindData");
        return Fail;
    }

    return Success;
}

bool Client::MakeGeneralData( PGeneralRequest pGR )
{
    if (pGR == NULL)
    {
        FLogger.information("wrong input data for making general data");
        return false;
    }
    strcpy_s(pGR->hardware_id, KEY_LEN+1, m_hwid.c_str());
    strcpy_s(pGR->product_key, KEY_LEN+1, m_pkey.c_str());
    strcpy_s(pGR->username,    USR_LEN+1, m_user.c_str());
    strcpy_s(pGR->password,    PWD_LEN+1, m_pass.c_str());
    strcpy_s(pGR->email,       EML_LEN+1, m_mail.c_str());

    return true;
}
